import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

class StudentScore {
    String name;
    String studentId;
    String course;
    double score;

    public StudentScore(String name, String studentId, String course, double score) {
        this.name = name;
        this.studentId = studentId;
        this.course = course;
        this.score = score;
    }
}

public class StudentScoreManagement {
    private static List<StudentScore> scores = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("=================================");
            System.out.println("欢迎使用学生成绩管理系统");
            System.out.println("=================================");
            System.out.println("请选择操作：");
            System.out.println("1. 记录学生成绩");
            System.out.println("2. 查询学生成绩");
            System.out.println("3. 统计课程成绩");
            System.out.println("4. 退出系统");
            int choice = 0;
            try {
                choice = scanner.nextInt();
                scanner.nextLine();
            } catch (InputMismatchException e) {
                System.out.println("输入的选项无效，请输入数字！");
                scanner.nextLine();
                continue;
            }
            switch (choice) {
                case 1:
                    addScore();
                    break;
                case 2:
                    queryScore();
                    break;
                case 3:
                    calculateScore();
                    break;
                case 4:
                    System.out.println("感谢使用学生成绩管理系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("输入的选项无效，请重新输入！");
            }
        }
    }

    private static void addScore() {
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入学生学号：");
        String studentId = scanner.nextLine();
        for (StudentScore score : scores) {
            if (score.studentId.equals(studentId)) {
                System.out.println("该学号已存在，请重新输入！");
                return;
            }
        }
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        double score = 0;
        while (true) {
            try {
                System.out.print("请输入成绩（0 - 100）：");
                score = scanner.nextDouble();
                scanner.nextLine();
                if (score >= 0 && score <= 100) {
                    break;
                } else {
                    System.out.println("成绩输入超出范围，请重新输入！");
                }
            } catch (InputMismatchException e) {
                System.out.println("输入的成绩不是有效的数字，请重新输入！");
                scanner.nextLine();
            }
        }
        scores.add(new StudentScore(name, studentId, course, score));
        System.out.println("成绩已成功记录！");
    }

    private static void queryScore() {
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        int queryChoice = 0;
        try {
            queryChoice = scanner.nextInt();
            scanner.nextLine();
        } catch (InputMismatchException e) {
            System.out.println("输入的选项无效，请输入数字！");
            return;
        }
        switch (queryChoice) {
            case 1:
                System.out.print("请输入学生姓名：");
                String name = scanner.nextLine();
                boolean foundByName = false;
                for (StudentScore score : scores) {
                    if (score.name.equals(name)) {
                        System.out.println("姓名：" + score.name + ", 学号：" + score.studentId + ", 课程：" + score.course + ", 成绩：" + score.score);
                        foundByName = true;
                    }
                }
                if (!foundByName) {
                    System.out.println("没有找到该学生的成绩记录！");
                }
                break;
            case 2:
                System.out.print("请输入学生学号：");
                String studentId = scanner.nextLine();
                boolean foundById = false;
                for (StudentScore score : scores) {
                    if (score.studentId.equals(studentId)) {
                        System.out.println("姓名：" + score.name + ", 学号：" + score.studentId + ", 课程：" + score.course + ", 成绩：" + score.score);
                        foundById = true;
                    }
                }
                if (!foundById) {
                    System.out.println("没有找到该学号的成绩记录！");
                }
                break;
            case 3:
                System.out.print("请输入课程名称：");
                String course = scanner.nextLine();
                boolean foundByCourse = false;
                for (StudentScore score : scores) {
                    if (score.course.equals(course)) {
                        System.out.println("姓名：" + score.name + ", 学号：" + score.studentId + ", 课程：" + score.course + ", 成绩：" + score.score);
                        foundByCourse = true;
                    }
                }
                if (!foundByCourse) {
                    System.out.println("没有找到该课程的成绩记录！");
                }
                break;
            default:
                System.out.println("输入的选项无效，请重新输入！");
        }
    }

    private static void calculateScore() {
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        double totalScore = 0;
        int count = 0;
        double maxScore = Double.MIN_VALUE;
        double minScore = Double.MAX_VALUE;

        for (StudentScore score : scores) {
            if (score.course.equals(course)) {
                totalScore += score.score;
                count++;
                if (score.score > maxScore) {
                    maxScore = score.score;
                }
                if (score.score < minScore) {
                    minScore = score.score;
                }
            }
        }

        if (count == 0) {
            System.out.println("没有找到该课程的成绩记录！");
        } else {
            double averageScore = totalScore / count;
            System.out.println(course + " 课程的平均成绩为：" + averageScore);
            System.out.println(course + " 课程的最高分为：" + maxScore);
            System.out.println(course + " 课程的最低分为：" + minScore);
        }
    }
}